package com.team.auth.config.security;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.team.auth.config.exception.SsoException;
import com.team.auth.config.exception.SsoExceptionEnums;
import com.team.auth.config.security.enums.SsoStrategy;
import com.team.auth.config.security.jwt.OauthUser;
import com.team.auth.entity.SsoUser;
import com.team.auth.service.SsoUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Map;

//只要注入spring容器就可以被查找到自动注入，也可以选择手动配置
@Service
@Slf4j
public class SsoUserDetails implements UserDetailsService {
    @Resource
    private SsoUserService ssoUserServiceImpl;
    @Autowired
    private SsoProperites ssoProperites;
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        User user = null;
        SsoUser ssoUser = null;
        ArrayList<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        grantedAuthorities.add(new SimpleGrantedAuthority("all"));
        SsoStrategy ssoStrategy = ssoProperites.getSsoStrategy();
        switch (ssoStrategy){
            case dependent:
                MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
                map.add("shopid","1");
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
                ResponseEntity<Map> mapResponseEntity = restTemplate.postForEntity(ssoProperites.getUserByUsernameUrl(), request, Map.class);
                Map body = mapResponseEntity.getBody();
                if (body ==null){
                    throw new SsoException(SsoExceptionEnums.USER_NO_EXIST);
                }
                String username = body.get("username").toString();
                user = new User(username,body.get("password").toString(),grantedAuthorities);
                ssoUser=new SsoUser();
                ssoUser.setName(username);
                break;
            case independent:
                EntityWrapper<SsoUser> ssoUserEntityWrapper = new EntityWrapper<>();
                SsoUser param = new SsoUser();
                param.setName(name);
                param.setStatus(1);
                ssoUserEntityWrapper.setEntity(param);
                ssoUser = ssoUserServiceImpl.selectOne(ssoUserEntityWrapper);
                if (ssoUser ==null){
                    throw new SsoException(SsoExceptionEnums.USER_NO_EXIST);
                }
                user = new User(ssoUser.getName(),ssoUser.getPassword(),grantedAuthorities);
                break;
                default:
                    throw new SsoException(SsoExceptionEnums.INVAILD_SSO_STRATEGY);
        }
//        OauthUser oauthUser = new OauthUser(ssoUser,user);  由于序列化问题，暂时弃用
        return user;
    }


}
